package com.clz.latte.ec.launcher;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;

import com.clz.latte.app.AccountManager;
import com.clz.latte.app.IUserChecker;
import com.clz.latte.delegate.LatteDaleGate;
import com.clz.latte.ec.R;
import com.clz.latte.ec.R2;
import com.clz.latte.ui.launcher.ILauncherFinishListener;
import com.clz.latte.ui.launcher.LauncherFinishTag;
import com.clz.latte.ui.launcher.LauncherScrollTag;
import com.clz.latte.utils.storage.LattePreference;
import com.clz.latte.utils.timer.BaseTimeTask;
import com.clz.latte.utils.timer.ITimeListener;

import java.text.MessageFormat;
import java.util.Timer;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/12/23.
 * 启动页
 */

public class LauncherDelegate extends LatteDaleGate implements ITimeListener {
    @BindView(R2.id.tv_skip)
    AppCompatTextView mTvTimer = null;
    private Timer mTimer = null;
    private int mCount = 5;
    private ILauncherFinishListener mILauncherFinishListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof ILauncherFinishListener) {
            mILauncherFinishListener = (ILauncherFinishListener) activity;
        }
    }

    @OnClick(R2.id.tv_skip)
    void onClickTimerView() {
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
            checkIsShowScroll();
        }
    }

    @Override
    public Object setLayOut() {
        return R.layout.delegate_launcher;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        initTimer();
    }

    private void initTimer() {
        if (mTimer == null) {
            mTimer = new Timer();
            BaseTimeTask task = new BaseTimeTask(this);
            mTimer.schedule(task, 0, 1000);
        }
    }

    //判断是否显示滑动启动页
    private void checkIsShowScroll() {
        if (!LattePreference.getAppFlag(LauncherScrollTag.HAS_FIRST_ENTER_APP.name())) {
            // getSupportDelegate().start(new LauncherScrollDelegate());
            startWithPop(new LauncherScrollDelegate());
        } else {
            //判断用户是否登录
            AccountManager.checkAccount(new IUserChecker() {
                /**
                 * 启动结束后，登录过了
                 */
                @Override
                public void onSignIn() {
                    if (null!=mILauncherFinishListener){
                        mILauncherFinishListener.onLauncherFinish(LauncherFinishTag.SIGN_IN);
                    }
                }

                /**
                 * 启动结束后，未注册登录
                 */
                @Override
                public void onNotSignIn() {
                    if (null!=mILauncherFinishListener){
                        mILauncherFinishListener.onLauncherFinish(LauncherFinishTag.NOT_SIGN_IN);
                    }
                }
            });
        }
    }

    @Override
    public void onTimer() {
        getProxyActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mTvTimer.setText(MessageFormat.format("跳过\n{0}", mCount--));
                if (mCount < 0) {
                    if (mTimer != null) {
                        mTimer.cancel();
                        mTimer = null;
                        checkIsShowScroll();
                    }
                }
            }
        });
    }
}
